/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Phạm Huy Đức Hàm gửi thông tin quản lý tiến trình
 */
public class eThreadManager implements Runnable {

   Socket mSocket;//Socket tiêu chuẩn truyền từ bên ngoài vào
   boolean loggedIn;//Lưu trạng thái đăng nhập server hay chưa

   @Override
   public void run() {
      try {
         Socket server = mSocket;
         InputStream inputstream = server.getInputStream();
         InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
         BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
         String string;
         while ((string = bufferedreader.readLine()) != null && !ThreadManager.isDestroy) {
            OutputStream outputstream = server.getOutputStream();
            OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
            BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
            if (string != null && string.equals("shutdown -f")) {//Nếu gặp lệnh tắt thì tự tắt ứng dung
               bufferedwriter.write("Shutting down ...\n");
               bufferedwriter.flush();
               bufferedwriter.close();
               ThreadManager.shutdown();
//               server.close();
//               System.out.flush();
//               System.exit(1);
            } else {//Không thì in ra
               String rt = ThreadManager.controlServer(string);
               bufferedwriter.write(rt);//In dữ liệu lấy được
               bufferedwriter.write("");//Thông báo đã hết thông tin
               bufferedwriter.newLine();//Kết dòng
               bufferedwriter.flush();
               bufferedwriter = null;
//               bufferedwriter.close();
               System.out.flush();
            }
         }
      } catch (Exception ex) {
         Logger.getLogger(eThreadManager.class.getName()).log(Level.SEVERE, null, ex);
      }
   }

   public eThreadManager(Socket mSocket) {
      this.mSocket = mSocket;
   }
}
